package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.service.TeamService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.TeamView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname TeamController
 * @Description TODO
 * @Date 2020/11/6 0:46
 * @Author by maixy
 */
@RestController
@RequestMapping("/v1/team")
public class TeamController extends ApiController {

    @Resource
    private TeamService teamService;

    /**
     * 已加入的团队列表
     *
     * @param request --
     * @return --
     */
    @Privilege
    public ApiResponse getTeamList(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        List<TeamView> teamViewList = teamService.getTeamList(userId);
        return ApiResponse.ok()
                .addValueToData("teamViewList", teamViewList);
    }

    /**
     * 查看团队邀请码
     *
     * @param request --
     * @return --
     */
    @Privilege
    public ApiResponse getInviteCode(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        return ApiResponse.ok()
                .addValueToData("inviteCode", teamService.getInviteCode(teamId, userId));
    }

    /**
     * 更新团队邀请码
     *
     * @param request --
     * @return --
     */
    @Privilege
    public ApiResponse updateInviteCode(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        return ApiResponse.ok()
                .addValueToData("inviteCode", teamService.updateInviteCode(teamId, userId));
    }

    /**
     * 加入团队
     *
     * @param request -- inviteCode 邀请码
     * @return --
     */
    @Privilege
    public ApiResponse joinTeam(ApiRequest request) {
        String inviteCode = request.getDataParamAsString("inviteCode");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (StringUtils.isBlank(inviteCode)) {
            throw new TipException("参数缺失");
        }
        teamService.joinTeam(userId, inviteCode);
        return ApiResponse.ok();
    }

    /**
     * 切换团队
     *
     * @param request -- inviteCode 邀请码
     * @return --
     */
    @Privilege
    public ApiResponse exchangeTeam(ApiRequest request) {
        Long currentTeamId = request.getDataParamAsLong("currentTeamId");
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (null == currentTeamId) {
            throw new TipException("参数缺失");
        }
        String newTeamId = teamService.exchangeTeam(teamId, userId, currentTeamId);
        return ApiResponse.ok()
                .addValueToData("teamId", newTeamId);
    }
}
